import {
  Shader,
  ShaderData,
  ShaderProperty,
  ShaderPool,
  ShaderProgram,
  ShaderProgramPool,
  ShaderMacro,
  ShaderMacroCollection,
  ShaderDataGroup,
  ShaderLib
} from "@nirvana/shaders";
import { Color } from "@nirvana/math";
import { WebGLCanvas } from "@nirvana/canvas";

const shaderMacroCollection: ShaderMacroCollection = new ShaderMacroCollection();
console.log(shaderMacroCollection);

// meta(viewport)、devicePixelRatio、canvas.width

// 使用使用 Canvas
let canvas = document.getElementById("engine-nirvana");
// console.log(canvas);

const webCanvas = new WebGLCanvas(canvas, {});
// webCanvas.fullScreen();
const engine = webCanvas.engine;
const gl = engine.gl;

const pvs = `
void main() {
  gl_Position = vec4(-1.0, -1.0, 0.0, 1.0);
  gl_PointSize = 10.0;
}
`;
const pfs = `
uniform vec4 u_color;
void main() {
  gl_FragColor = vec4(u_color);
}
`;

const point = Shader.create("point", pvs, pfs);
console.log(point, Shader.find("point"));
const pool: ShaderProgramPool = new ShaderProgramPool();
const pprogram: ShaderProgram = point.getShaderProgram(gl, shaderMacroCollection, pool);
console.log(pprogram);

const shaderData: ShaderData = new ShaderData(ShaderDataGroup.Material);
console.log(ShaderDataGroup, shaderData);
shaderData.setColor("u_color", new Color(1, 0, 0, 1)); // { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }

engine.update = (gl) => {
  // console.log('--');
  gl.clearColor(0.0, 0.0, 0.0, 1.0);
  // 用上面指定的颜色清除缓冲区
  gl.clear(gl.COLOR_BUFFER_BIT);
  // 点start 一
  pprogram.bind();
  pprogram.uploadAll(pprogram.otherUniformBlock, shaderData);
  gl.drawArrays(gl.POINTS, 0, 1);
  // 点end
};
